package com.novel.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;


@Slf4j
@Component
public class RedisUtils {

    @Autowired
    private JedisPool jedisPool;

    /**
     * redis自增
     */
    public void incrSum(String key) {
        Jedis jedis = jedisPool.getResource();
        jedis.incrBy(key, 1L);
        returnJedisToPool(jedis);
    }

    /**
     * redis自增
     */
    public void incrSum(String key, long incr) {
        Jedis jedis = jedisPool.getResource();
        jedis.incrBy(key, incr);
        returnJedisToPool(jedis);
    }

    /**
     * 给hash中的某一个字段自增
     */
    public void hashIncrSum(String key, String field) {
        Jedis jedis = jedisPool.getResource();
        jedis.hincrBy(key, field, 1);
        returnJedisToPool(jedis);
    }

    /**
     * 删除相关的缓存
     */
    public void deleteByKey(String key) {
        Jedis jedis = jedisPool.getResource();
        jedis.del(key);
        returnJedisToPool(jedis);
    }


    /**
     * 给hash中的某一个字段自增
     */
    public void hashIncrSum(String key, String field, int incr) {
        Jedis jedis = jedisPool.getResource();
        jedis.hincrBy(key, field, incr);
        returnJedisToPool(jedis);
    }

    /**
     * String类型设置
     */
    public void stringSet(String key, String value, int seconds) {
        Jedis jedis = jedisPool.getResource();
        jedis.set(key, value);
        jedis.expire(key, seconds);
        returnJedisToPool(jedis);
    }

    /**
     * String类型设置
     */
    public void stringSet(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        jedis.set(key, value);
        returnJedisToPool(jedis);
    }

    /**
     * String类型获取
     */
    public String stringGet(String key) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.get(key);
        returnJedisToPool(jedis);
        return result;
    }

    /**
     * Hash类型设置
     */
    public void hashSet(String key, String field, String value, int seconds) {
        Jedis jedis = jedisPool.getResource();
        jedis.hset(key, field, value);
        expireKeyTimeBySeconds(jedis, key, seconds);
    }

    /**
     * Hash类型设置
     */
    public void hashSet(String key, String field, String value) {
        Jedis jedis = jedisPool.getResource();
        jedis.hset(key, field, value);
        returnJedisToPool(jedis);
    }

    /**
     * Hash类型获取
     */
    public String hashGet(String key, String field) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.hget(key, field);
        returnJedisToPool(jedis);
        return result;
    }

    /**
     * List设置
     */
    public void listSet(String key, int seconds, String... values) {
        Jedis jedis = jedisPool.getResource();
        jedis.lpush(key, values);
        expireKeyTimeBySeconds(jedis, key, seconds);
    }

    /**
     * List设置
     */
    public void listSet(String key, String... values) {
        Jedis jedis = jedisPool.getResource();
        jedis.lpush(key, values);
        returnJedisToPool(jedis);
    }

    /**
     * List获取
     */
    public List<String> listGet(String key) {
        Jedis jedis = jedisPool.getResource();
        //获取所有集合
        List<String> list = jedis.lrange(key, 0, -1);
        returnJedisToPool(jedis);
        return list;
    }

    /**
     * List获取
     */
    public List<String> listGet(String key, int star, int end) {
        Jedis jedis = jedisPool.getResource();
        List<String> list = jedis.lrange(key, star, end);
        returnJedisToPool(jedis);
        return list;
    }


    /**
     * 设置key的过期时间
     */
    private void expireKeyTimeBySeconds(Jedis jedis, String key, int seconds) {
        //设置过期时间
        jedis.expire(key, seconds);
        //回收连接
        returnJedisToPool(jedis);
    }


    /**
     * 把连接放回到连接池中
     */
    private void returnJedisToPool(Jedis jedis) {
        if (jedis != null) {
            jedisPool.returnResource(jedis);
        }
    }
}